<?php
namespace app\backend\controllers;
use app\backend\common\AdminBaseController;
use app\backend\models\RoleGroupModel;
use app\backend\models\RoleMenuModel;
use lib\db\DbTool;
use lib\helper\error;
use lib\redis\RedisTool;
use lib\Tool\Tools;

class RoleController extends AdminBaseController {


    public function actionList(){
        $RoleGroupModel = new RoleGroupModel();
        $list = $RoleGroupModel->getGroup();
        $RoleMenuModel = new RoleMenuModel();
        $roleData = $RoleMenuModel->getRoleGroup();
        $this->assign('data',['list'=>$list,'role'=>$roleData]);
        return $this->display('role/list');
    }

    public function actionAdd(){
        $post = Tools::My()->request()->post();
        if(empty($post)){
            return $this->display('role/add');
        }
        $code = RoleGroupModel::find()->insert(['group_name'=>$post['group_name'],'group_remark'=>$post['group_remark'],'add_time'=>time()]);
        if ($code > 0) {
            error::_plainError(['message' => '添加成功']);
        }
        error::_plainError(['message' => '添加失败']);
    }

    public function actionRoleMenu(){
        $group_id = Tools::My()->request()->get('group_id',0);
        $post = Tools::My()->request()->post();
        $RoleMenuModel = RoleMenuModel::find();
        if (empty($post)){
            $RoleMenuData = $RoleMenuModel->where([['=','group_id',$group_id]])->all();
            error::_plainError(['data'=>$RoleMenuData,'message' => '查询成功'],true);
        }
        $menu_id = $post['menu_id'];
        $RoleMenuModel->where([['=','group_id',$group_id]])->deleteAll();
        foreach ($menu_id as $k=>$v) {
            $RoleMenuModel->insert(['group_id'=>$group_id,'menu_id'=>$v]);
        }
        error::_plainError(['code'=>'200','message' => '添加成功']);
    }

}